<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>折半查找又叫二分查找</title>
	</head>
	<body>
	</body>

</html>
<script>
	var sortArr = new Array(1, 4, 7, 9, 11, 12, 21, 26, 33);
	function f(arr, n) {
		//assume : arr has been sorted    
		var low = 0;
		var high = arr.length - 1;
		var mid = (low + high) / 2;
		
        console.log(low);
		console.log(high);
		console.log(mid);

		while(high - low > 1) {
			// 掐头
			if(n == arr[low]) {
				return low;
			
			}
			// 去尾
			if(n == arr[high]) {
				return high;
			}
			// 找中间
			if(n == arr[mid]) {
				return mid;
			}
			if(n > arr[mid]) {
				console.log("------------------------------------------->");
				// 重新定义数组,把比较后的中心位置变为假设数组的头部
				low = mid;
				console.log(low);
				// 重新寻找假定数组的中心坐标,然后再次进行while循环
				mid = (low + high) / 2;
				console.log(mid);
			}
			if(n < arr[mid]) {
				console.log("------------------------------------------->");
				high = mid;
				console.log(high);
				mid = (low + high) / 2;
				console.log(mid);
			}
			console.log("------------------------------------------->");
			console.log(mid);
			console.log(high);
			console.log(low);
		}
	}
	f(sortArr, 12);
	

	console.log(f(sortArr, 12));

	//选择排序
	      function sort(array) {
		var length = array.length;
		for (var i = 0; i < length ; i++) { //缩小选择的范围
			var  min = array[i]; //假定范围内第一个为最小值
			var index = i;//记录最小值的下标
			for (var j = i + 1; j< length; j++) {//在范围内选取最小值
				if (array[j] < min) {
					min = array[j];
					index = j;
				}
			}
		if (index != i) {//把范围内最小值交换到范围内第一个
			var temp = array[i];
			array[i] = array[index];
			array[index] = temp;
			}	 
			
		}
		return array;
	}
	var arr = [2, 4, 3, 1, 7, 5, 6, 9, 8, 0];
	

	var res =  sort(arr);
	console.log(res);
</script>